import { DXMResponse } from "./common";
import { invoke } from "@tauri-apps/api/core";
const dxc_name = "RtdataSubscriberService";
const dxc_version = "0.0.1";
export enum FQType {
  NA = 0,
  FWARD = 1,
  BWARD = 2,
}
export enum Market {
  NOMARKET = 0,
  SZSE = 1,
  SSE = 2,
  SHFE = 3,
  DCE = 4,
  CZCE = 5,
  CFFEX = 6,
  INE = 7,
  GFEX = 8,
}
export type MinOrDayData = {
  datetime: number;
  open: number;
  high: number;
  low: number;
  close: number;
  settlement: number;
  volume: number;
  total_turnover: number;
  open_interest: number;
  average: number;
};
export type RTDataRequest = {
  market: Market;
  code: string;
  filled_up: boolean;
  fq: FQType;
  fre_num: number;
};
export type SubscribeRTDataRequest = {
  market: Market;
  code: Array<string>;
};
export type CodeInfoList = {
  datas: Array<CodeInfo>;
};
export type CodeInfo = {
  market: Market;
  code: string;
  trading_code?: string;
  name: string;
  short_en: string;
  listed_date: number;
  de_listed_date: number;
  trading_hours: Array<number>;
  contract_multiple: number;
  min_tick: number;
};
export type SearchCodeRequest = {
  keyword: string;
};
export type MinOrDayDataList = {
  datas: Array<MinOrDayData>;
};
export async function SubscribeRtData(param: SubscribeRTDataRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "SubscribeRtData",
      params: param,
    },
  });
  return ret as DXMResponse<{}>;
}

export async function CancelSubscribeRtData(param: SubscribeRTDataRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "CancelSubscribeRtData",
      params: param,
    },
  });
  return ret as DXMResponse<{}>;
}

export async function GetRtMinData(param: RTDataRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "GetRtMinData",
      params: param,
    },
  });
  return ret as DXMResponse<MinOrDayDataList>;
}

export async function SearchCode(param: SearchCodeRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "SearchCode",
      params: param,
    },
  });
  return ret as DXMResponse<CodeInfoList>;
}
